package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.SysMenu;
import cn.com.dcsgo.dto.SysMenuAddDTO;
import cn.com.dcsgo.dto.SysMenuUpdateDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.SysMenuService;
import cn.com.dcsgo.utils.AuthUtil;
import cn.com.dcsgo.vo.MenuPermsVO;
import cn.com.dcsgo.vo.SysMenuVO;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台系统菜单管理相关接口
 *
 * @author Dcsgo
 * @since 2025/9/22 15:19
 */
@RestController
@RequestMapping("/sys/menu")
@Tag(name = "后台系统菜单管理相关接口")
public class SysMenuController {
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 获取登录用户可访问的菜单和权限
     *
     * @return 登录用户可访问的菜单和权限信息
     */
    @GetMapping("nav")
    @Operation(summary = "获取用户可访问的菜单和权限")
    public Result<MenuPermsVO> getMenuAndPerms() {
        List<SysMenu> menuList = sysMenuService.getMenuListByUserId(AuthUtil.getUserId());
        MenuPermsVO menuPermsVO = new MenuPermsVO(menuList, AuthUtil.getPerms());
        return Result.success(menuPermsVO);
    }

    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @Operation(summary = "获取可访问目录菜单列表", description = "登录用户所拥有的角色关联的目录、菜单，不具有层级关系，不包含按钮")
    public Result<List<SysMenu>> getDirectoryAndMenuList() {
        List<SysMenu> list = sysMenuService.getDirectoryAndMenuListByUserId(AuthUtil.getUserId());
        return Result.success(list);
    }

    @GetMapping("table")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @Operation(summary = "获取可访问菜单列表", description = "登录用户所拥有的角色关联的目录、菜单、按钮，不具有层级关系")
    public Result<List<SysMenu>> getAll() {
        List<SysMenu> list = sysMenuService.getMenusByUserId(AuthUtil.getUserId());
        return Result.success(list);
    }

    @GetMapping("info/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    @Operation(summary = "获取菜单信息")
    public Result<SysMenuVO> getMenuInfo(@PathVariable Long menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        SysMenuVO sysMenuVO = new SysMenuVO();
        BeanUtil.copyProperties(menu, sysMenuVO);
        return Result.success(sysMenuVO);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sys:menu:save')")
    @Operation(summary = "新增菜单（权限）")
    public Result<Void> saveMenu(@Validated @RequestBody SysMenuAddDTO sysMenuAddDTO) {
        int row = sysMenuService.save(sysMenuAddDTO);
        return Result.handle(row > 0);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sys:menu:update')")
    @Operation(summary = "更新菜单信息")
    public Result<Void> updateMenu(@Validated @RequestBody SysMenuUpdateDTO sysMenuUpdateDTO) {
        int row = sysMenuService.update(sysMenuUpdateDTO);
        return Result.handle(row > 0);
    }

    @DeleteMapping("{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    @Operation(summary = "删除菜单", description = "不允许删除具有子菜单的菜单")
    public Result<Void> removeMenu(@PathVariable Long menuId) {
        int row = sysMenuService.waryRemoveById(menuId);
        return Result.handle(row > 0);
    }
}
